﻿using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Excel;
using Magicodes.ExporterAndImporter.Excel.AspNetCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.AuthorizationUserManager.Dtos;
using NP.BPMReportPlatform.Entities.NPPlatformManage;
using NP.BPMReportPlatform.Reports.OrderMaterialDetail.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.AuthorizationUserManager
{
    [Authorize]
    [ApiController]
    [Route("api/PlatformManage/Role")]
    public class PlatformManageRoleController : BPMReportPlatformController
    {
        private readonly IPlatformManageRoleService _service;
        private readonly IExcelImporter _importer;

        public PlatformManageRoleController(IPlatformManageRoleService service, IExcelImporter importer)
        {
            _service = service;
            _importer = importer;
        }

        /// <summary>
        /// 获取平台管理角色信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetPageList")]
        public async Task<PagedResultDto<PermissionsRoles>> GetPageList([FromBody] PlatformManageRoleInput input)
        {
            return await _service.GetPageList(input);
        }

        /// <summary>
        /// 导出平台管理角色信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ExportExcel")]
        public async Task<XlsxFileResult<PermissionsRoles>> ExportExcel([FromBody] PlatformManageRoleInput input)
        {
            var rst = await _service.ExportExcel(input);
            return new XlsxFileResult<PermissionsRoles>(rst, "平台管理角色信息导出Excel");
        }

        /// <summary>
        /// 查询已绑定角色用户数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetUserRoleList")]
        public async Task<PagedResultDto<PlatformManageRoleUserDto>> GetUserRoleList([FromBody] PlatformManageRoleUserInput input)
        {
            return await _service.GetUserRoleList(input);
        }

        /// <summary>
        /// 导出已绑定角色用户数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ExportRoleUserExcel")]
        public async Task<XlsxFileResult<PlatformManageRoleUserDto>> ExportRoleUserExcel([FromBody] PlatformManageRoleUserInput input)
        {
            var rst = await _service.ExportRoleUserExcel(input);
            return new XlsxFileResult<PlatformManageRoleUserDto>(rst, "已绑定角色用户数据导出Excel");
        }

        /// <summary>
        /// 删除角色绑定用户关系
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("DeleteRelations")]
        public async Task DeleteRelations([FromBody] PlatformManageRoleUserDeleteInput input)
        {
            await _service.DeleteRelations(input);
        }

        /// <summary>
        /// 添加角色绑定用户关系
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("BindUsers")]
        public async Task BindUsers([FromBody] PlatformManageBindUsersInput input)
        {
            await _service.BindUsers(input);
        }

        /// <summary>
        /// 获取平台管理用户信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetUserPageList")]
        public async Task<PagedResultDto<PlatformManageUserDto>> GetUserPageList([FromBody] PlatformManageRoleUserInput input)
        {
            return await _service.GetUserPageList(input);
        }

        /// <summary>
        /// 导出平台管理用户信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ExportUserExcel")]
        public async Task<XlsxFileResult<PlatformManageUserDto>> ExportUserExcel([FromBody] PlatformManageRoleUserInput input)
        {
            var rst = await _service.ExportUserExcel(input);
            return new XlsxFileResult<PlatformManageUserDto>(rst, "平台管理用户信息导出Excel");
        }

        /// <summary>
        /// 获取角色绑定权限树结构数据
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetAuthTreeData")]
        public async Task<PlatformManageRoleMenuTreeData> GetAuthTreeData([FromQuery] Guid roleId)
        {
            return await _service.GetAuthTreeData(roleId);
        }

        /// <summary>
        /// 导出导入模板
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ExportTemplateExcel")]
        public async Task<XlsxFileResult<ImportTemplateModel>> ExportTemplateExcel()
        {
            var rst = await _service.ExportTemplateExcel();
            return new XlsxFileResult<ImportTemplateModel>(rst, "导出导入模板");
        }

        /// <summary>
        /// 导入授权数据
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ImportUserRoleExcel")]
        public async Task<ImportResultExt<ImportTemplateModel>> ImportUserRoleExcel(IFormFile file)
        {
            if (file == null)
            {
                var res = new ImportResultExt<ImportTemplateModel>
                {
                    Exception = new Exception("请上传文件！")
                };
                return res;
            }
            var stream = file.OpenReadStream();
            var rst = await _importer.Import<ImportTemplateModel>(stream);
            stream.Close();
            if (rst.HasError)
            {
                var resp = new ImportResultExt<ImportTemplateModel>()
                {
                    Exception = rst.Exception,
                    RowErrors = rst.RowErrors,
                    TemplateErrors = rst.TemplateErrors
                };
                return resp;
            }
            else
            {
                return await _service.ImportUserRoleExcel(rst.Data.ToList());
            }
        }
    }
}
